package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootTableTitle;
import org.footballmanager.pandafootball.system.domain.enums.JNFL;
import org.footballmanager.pandafootball.system.form.impl.FootTableTitleSearchForm;
import org.footballmanager.pandafootball.system.mapper.FootTableTitleMapper;
import org.footballmanager.pandafootball.system.service.FootTableTitleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class FootTableTitleServiceImpl extends ServiceImpl<FootTableTitle> implements FootTableTitleService {
    @Autowired
    private FootTableTitleMapper footTableTitleMapper;

    /**
     * 根据表名称查询表头列表
     * @param tableName 表名称
     * @return 表头列表
     */
	@Override
	public Map<String, String> findByTableName(String tableName) {
		Example example = new Example(FootTableTitle.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("tableName", tableName);
		example.orderBy("sort asc");
		List<FootTableTitle> list = this.findall(example);
		if(list != null && !list.isEmpty()){
            return list2Map(list);
        }
        return null;
	}

    private Map<String, String> list2Map(List<FootTableTitle> list) {
        Map<String, String> map = new HashMap<>();
        if (list != null) {
            for (FootTableTitle footTableTitle: list) {
                map.put("nl" + footTableTitle.getSort(), footTableTitle.getName());
            }
        }
        return map;
    }

    /**
     * 根据表名称查询表头列表
     *
     * @param tableName 表名称
     * @param jnfl      技能分类
     * @return 表头列表
     */
    @Override
    public List<FootTableTitle> findByTableNameAndJnfl(String tableName, JNFL jnfl) {
        FootTableTitle footTableTitle = new FootTableTitle();
        footTableTitle.setTableName(tableName);
        footTableTitle.setJnfl(jnfl);
        return footTableTitleMapper.findByTableNameAndJnfl(footTableTitle);
    }

    /**
     * 根据列表头查询表名
     *
     * @param name 表头
     * @return 表名列表
     */
    @Override
    public List<String> findTableByName(String name) {
        List<String> list = new ArrayList<>();
        Example example = new Example(FootTableTitle.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("name", name);
        example.orderBy("sort asc");
        List<FootTableTitle> footTableTitles =  this.findall(example);
        if(footTableTitles != null && !footTableTitles.isEmpty()){
            for(FootTableTitle footTableTitle: footTableTitles){
                list.add(footTableTitle.getTableName());
            }
        }
        return list;
    }

    /**
     * 查询
     *
     * @param entityname               实体类名
     * @param map                      参数
     * @param footTableTitleSearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootTableTitleSearchForm footTableTitleSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footTableTitleSearchForm);
        Page<FootTableTitle> page = PageHelper.startPage(footTableTitleSearchForm.getPageIndex(), footTableTitleSearchForm.getPageSize());
        List<FootTableTitle> list = this.findall(footTableTitleSearchForm.buildSearchCommond());
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 删除所有
     *
     * @param footTableTitleSearchForm 条件
     * @param request                  请求
     */
    @Override
    public void deleteAll(FootTableTitleSearchForm footTableTitleSearchForm, HttpServletRequest request) {
        if(footTableTitleSearchForm.getSelectIds() != null){
            String[] result = footTableTitleSearchForm.getSelectIds().split(Constants.COMMA);
            for(String id: result){
                this .delete(Long.parseLong(id), request);
            }
        }
    }
}
